<?php
	
	include('cms.php');
	
	$level = Session::level();
	
	if(isset($_GET['idcontent'])){
		
		Log::add(OPEN, $_GET['idcontent']);
		
		echo json_encode( DataLatte::oneof('Content', "SELECT * FROM cmsContent WHERE idcontent = '" . $_GET['idcontent'] . "'"));
	}elseif(isset($_GET['recent'])){
		
		echo json_encode( DataLatte::arrayof('Content', "SELECT idcontent, title FROM cmsContent INNER JOIN cmsLog ON cmsLog.cmsContent_idcontent = cmsContent.idcontent WHERE action = " . OPEN . " AND cmsContent.cmsUser_iduser = '" . Session::user() . "' GROUP BY idcontent LIMIT 10"));
		
	}else{
	
		$arr1 = array();
		$arr2 = array();
	
		if(isset($_GET['idcategory']) && !empty($_GET['idcategory']) ){
			$arr1 = DataLatte::arrayof('Category', "SELECT * FROM cmsCategory WHERE cmsCategory_idcategory = '" . $_GET['idcategory'] . "' ORDER BY name");
			$arr2 = DataLatte::arrayof('Content', "SELECT idcontent, title, modified FROM cmsContent WHERE cmsCategory_idcategory = '" . $_GET['idcategory'] . "' AND editlevel <= '$level'  ORDER BY modified DESC") ;
		}else{
			$arr1 = DataLatte::arrayof('Category', "SELECT * FROM cmsCategory WHERE cmsCategory_idcategory IS NULL ORDER BY name");
			$arr2 = DataLatte::arrayof('Content', "SELECT idcontent, title, modified FROM cmsContent WHERE cmsCategory_idcategory AND editlevel <= '$level' IS NULL
									   ORDER BY modified DESC") ;
		}
		
		
		echo json_encode( array_merge($arr1, $arr2) );
		
	}
	
?>